package UILayer;
import java.util.Scanner;
import CtrlLayer.*;
import ModelLayer.PartOrder;
import ModelLayer.RentItem;
import ModelLayer.SaleItem;
import ModelLayer.SaleOrder;
import ModelLayer.Employee;

public class SaleUI
{
//	private PersonCtrl PC;
	private CtrlLayer CT;
	private OrderCtrl OC;
//	private ItemCtrl IC;
	private EmployeeUI EU;
	private ItemUI IU;
	private Employee myemployee;
//	private SaleItem mysaleitem; 
	
    public SaleUI()
    {
//    	PC = new PersonCtrl();
//		IC = new ItemCtrl();
		OC = new OrderCtrl();
		CT = new CtrlLayer();
		IU = new ItemUI();
		EU = new EmployeeUI();
    }
    
    public void SaleMenu()
    {   
    	boolean exit = false;
        while (!exit) //! means while exit not is true (that is: false)
        {
        	int choise = writeSalesMenu();
        	switch(choise)
        	{
        		case 1: createSaleOrder(); break;
        		case 2: printSaleOrder(); break;
        		case 3: partorder2order(); break;
        		case 99: exit = true; break;
        	}//end switch
        }//end while
    }
    
    private int writeSalesMenu()
    {
        //creates an object keyboard to read data from the keyboard
    	Scanner keyboard = new Scanner(System.in);
    	System.out.println(" *** Salg Menu ***");
    	System.out.println(" (1) Opret salgs ordre ");
    	System.out.println(" (2) Soeg salgs ordre ");
    	System.out.println(" (3) tilfoej parto4rder til ordre ");
    	System.out.println(" (99) Tilbage");
    	System.out.print("\n Make your choise: ");
    	int choise = keyboard.nextInt();
    	return choise;
    }
    
    public void createSaleOrder()
    {
    	int ordernr = inputintnumber("Ordre nr ");
    	String date = CT.getDateString();
    	double totalPrice = 0;
    	int deliveryDiscount = 0;
    	String phone = inputtext("Medarbejder Tlf ");
        myemployee = EU.getEmployee(phone);
//    	myemployee = null;
    	String deliveryDate = inputtext("Leverings dato ");
    	double offer = 0;
    	OC.createSaleOrder(ordernr, date, totalPrice, myemployee, deliveryDiscount, deliveryDate, offer);
    }
    
    public void printSaleOrder()
    {
    	int ordernr = inputintnumber("Ordre nr ");
    	SaleOrder mysaleorder = OC.searchSaleOrder(ordernr);
    	System.out.println();
    	System.out.println("Ordre nr  : " + mysaleorder.getOrdernr());
    	System.out.println("Ordre dato: " + mysaleorder.getDate());
    	System.out.println("Total pris: " + mysaleorder.getTotalPrice());
    	System.out.println("Saelger   : " + mysaleorder.getEmployee());
    	System.out.println("Rabat     : " + mysaleorder.getDeliveryDiscount());
    	System.out.println("Ordre nr  : " + mysaleorder.getDeliveryDate());
    	System.out.println("Tilbud    : " + mysaleorder.getOffer());
    	System.out.println();
    	System.out.println(" Antal " + " type " + " Pris ");
    	for(PartOrder partorder : mysaleorder.getPartOrderList())
    	{
    		System.out.print("   " + partorder.getAmount());
    		System.out.print(" : " + partorder.getSaleItem().getDescription());
    		System.out.println(" : " + partorder.calculateSubTotal());
    	}
    	System.out.println();
    }
    
    public void partorder2order()
    {
    	int ordernr = inputintnumber("Ordre nr ");
    	int amount = inputintnumber("Antal ");
    	int itemnr = inputintnumber("Varenr ");
    	SaleItem mysaleitem = IU.getSaleItem(itemnr);
    	RentItem rentItem = null;
    	double subTotal = 0;
    	int quantityDiscount = 0;
    	OC.addPartOrder(ordernr, amount, mysaleitem, rentItem, subTotal, quantityDiscount);
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return str Return the input from the keyboard 
     */
    private String inputtext(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        return str;
    }
     
    private int inputintnumber(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        int no = Integer.parseInt(str.trim());  // convert String 2 long
        return no;
    }
}